import SessionUserUtil from '../../../data/session/sessionUser'
import PagePromoterUtil, { PagePromoter } from '../../../data/common/pagePromoter';
import AppSceneUtil from '../../../data/common/appScene';
import SessionUtil from "../../../data/session/session"
const Common = require('../../../utils/common.js');
Page({
    data: {
        imageSize: [],
        salesman_id:"",
        id:"",
        isFailure : false,
        inputdisabled:false
    },
    onLoad: function (options) {
        // 开启转发按钮
        PagePromoterUtil.showShareMenu(true).then()
        SessionUtil.getInstance().then(session => {
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this)
            /**获取用户当前顾问名字 */
            if (typeof options === 'object' && Object.keys(options).length > 0) {
                console.log(options);
                if (options.scene) {/**小程序码进来的 */
                    let scene = decodeURIComponent(options.scene), datajson = {};
                    let sj = scene.split('&');
                    for(let i in sj){
                        let sjj = sj[i].split('=');
                        datajson[sjj[0]] = sjj[1];
                    }
                    this.setData({
                        salesman_id: datajson.salesman_id,
                        id: datajson.id,
                        scene: options.scene
                    })
                } else {/**分享进来的 */
                this.setData({
                    salesman_id: options.salesman_id,
                    id: options.id
                })
                }
                Common.getConlantInfo(this.data.salesman_id, false, function (res) {
                    wx.setNavigationBarTitle({
                        title: res.info.name + '邀您参加品鉴会'
                    })
                })

                this.getUserInfo();
                //获取品鉴会信息
                this.judgeLookInvitation();
            }
        })
    },
    onShow: function(){
        SessionUtil.getInstance().then(()=>{
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this,false)
        })
    },
    /**
     * @description 获取用户信息，如果用户是登录状态，
     * 直接把用户名和手机号赋到表单并且表单信息不可修改，
     * 否则表单为空
     * @method getUserInfo
     * @returns void
     *
     */
    getUserInfo: function () {
        SessionUserUtil.getInstance().then(user => {
            if(user.userId > 0){
                this.setData({
                    name: user.realName,
                    phone: user.phoneNumber,
                    inputdisabled:"true"
                })
            }
            if (typeof callback === 'function') callback()
        }).catch(error => {
            console.log(error.message, error)
        })
    },

    /**
     * @description 获取品鉴会信息
     * @method judgeLookInvitation
     * @returns void
     *
     */
    judgeLookInvitation: function () {
        var page = this;
        Common.my_request({
            url: '/AppMaster/Taste/judgeLookInvitation',
            data: {
                id: page.data.id,
                salesman_id: page.data.salesman_id,
                scene: page.data.scene
            },
            success: function (res) {
                if (res.data.result == 0) {
                    var data = res.data.data
                    var joined_num = data.joined_num,
                        total_num = data.total_num;

                    page.setData({
                        joined: data,
                        percentage: page.GetPercent(joined_num, total_num)
                    })

                    /**品鉴会失效 */
                    if (data.continue_state == 0){
                        page.setData({
                            isFailure : true
                        })
                        
                    }
                }
            }
        });
    },

    /**
     * @description 进度条
     * @method GetPercent
     * @returns void
     *
     */
    GetPercent: function (num, total) {
        var page = this;
        num = parseFloat(num);
        total = parseFloat(total);
        if (isNaN(num) || isNaN(total)) {
            return "-";
        }
        if (total <= 0 || (Math.round(num / total * 10000) / 100.00 == 0)) {
            page.setData({
                light_spot : false
            })
        } else {
            page.setData({
                light_spot: true
            })
        }
        return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00 + "%");
    },

    /**
     * @description 提交品鉴会报名信息
     * @method submit
     * @returns void
     *
     */
    submit: function () {
        var page = this,
            name = page.data.name,
            phone = page.data.phone,
            note = page.data.mark,
          phonetest = /^0?1\d{10}$/;
        if (!name) {
            Common.showToast('请输入姓名', false);
            return false;
        }
        if (!phone) {
            Common.showToast('请输入手机号码', false);
            return false;
        } else if (!phonetest.test(phone)) {
            Common.showToast('请输入正确的手机号码', false);
            return false;
        }
        Common.my_request({
            url: '/AppMaster/Taste/judgeJoin',
            data: {
                id: page.data.id,
                salesman_id: page.data.salesman_id,
                name: name,
                phone: phone,
                note: note
            },
            success: function (res) {
                if (res.data.result == 0) {
                    var json = {};
                    json.state_msg = "报名成功";
                    json.continue_state_msg = res.data.msg;
                    page.setData({
                        isFailure:true,
                        joined: json
                    })
                } else {
                    var json = {};
                    json.state_msg = "报名失败";
                    json.continue_state_msg = res.data.message;
                    page.setData({
                        isFailure: true,
                        joined: json
                    })
                }
            }
        });
    },

    /**输入框value更新 */
    updateName:function(e){
        this.setData({
            name: e.detail.value
        })
    },
    updatePhone: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    updateMark: function (e) {
        this.setData({
            mark: e.detail.value
        })
    },

    /**页面加载进来调整图片大小 */
    imageLoad: function (e) {
        Common.imageLoadList(e, this);
    },

    /**取消下拉刷新 */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },

    /**去首页 */
    goIndex :function(){
        wx.switchTab({
            url: '/pages/index/index',
        })
    },

    /**分享 */
    onShareAppMessage: function () {
        // 构造并返回自定义数据
        return (new PagePromoter( this.route, { salesman_id : this.data.salesman_id, id:this.data.id }))
        .setShareTitle('品鉴会')
        .buildShareInfo()
    }
})